﻿/*-------------------------------------------------------------------------
 * 版权所有：	黑客马拉松
 * 作者：		罗毅
 * 联系方式：	whluoyi@gmail.com
 * 创建时间：	2016/10/29 11:23:16
 * 版本号：		v1.0
 * 用途描述：	日志代理
 *  -------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;


namespace SuperMarieApi.Log.LogAgent.Xml
{
    /// <summary>
    /// Xml读取参数
    /// </summary>
    internal class XReader
    {
        #region 构造函数
        public XReader(string filePath, XmlNode node, string nodeName)
        {
            this.xmlFilePath = filePath;
            this.NodeName = nodeName;
            this.node = node;
        }
        public XReader(string filePath, XmlNode node) : this(filePath, node, "*")
        {
        }
        public XReader(string filePath, string nodeName) : this(filePath, null, nodeName)
        {
        }
        public XReader(XmlNode node, string nodeName) : this(string.Empty, node, nodeName)
        {
        }
        public XReader(XmlNode node) : this(string.Empty, node, "*")
        {
        }
        public XReader(string filePath) : this(filePath, null, "*")
        {
        }
        public XReader() : this(string.Empty, null, "*")
        {
        }
        #endregion

        #region Node
        private string xmlFilePath = string.Empty;
        /// <summary>
        /// 通过xml获取节点
        /// </summary>
        /// <returns></returns>
        private XmlNode getNode()
        {
            if (!string.IsNullOrWhiteSpace(this.xmlFilePath))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(this.xmlFilePath);
                if (doc.ChildNodes.Count > 0)
                {
                    return doc.ChildNodes[doc.ChildNodes.Count - 1];
                }
                return doc;
            }
            return null;
        }
        private XmlNode node = null;
        /// <summary>
        /// Xml读取节点
        /// </summary>
        public XmlNode Node
        {
            get
            {
                if (node == null)
                {
                    node = getNode();
                }
                return node;
            }
        }
        #endregion

        /// <summary>
        /// 读取Node的名称
        /// </summary>
        public string NodeName { get; private set; }
    }
}
